﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseDll;
using System.Threading;
using System.Reflection;

namespace MotionIoLib
{
    public enum SixRobotAlarm
    {
        六轴机器人MoveJ超时 = 204,
        六轴机器人MoveL超时,
    }

    public abstract class BaseSixRobot
    {
        public BaseSixRobot(string strName, params object[] RobotParam)
        {

        }
        public string strName = "";
        public Dictionary<string, int> dic_IoIn = new Dictionary<string, int>();
        public Dictionary<string, int> dic_IoOut = new Dictionary<string, int>();
        //设置负载
        public abstract bool Open();

        public virtual bool ReConnect()
        {
            return true;
        }
        //读取输入信号
        public abstract bool ReadDigitalInPort(int  n_Index);
        //读取输出信号
        public abstract bool ReadDigitalOutPort(int n_Index);
        //设置输出信号
        public abstract bool SetDigitalOutputPort(int n_Index, bool b_On);
        //获取当前位置--大地坐标系
        public abstract SixRobotPoint GetCurrentPos_World();
        //获取当前位置--关节坐标系
        public abstract SixRobotJoint GetCurrentPos_Joint();
        public abstract bool MoveLine(SixRobotPoint point, bool bmamual, int Speed = 100, int BlendValue = 500);
        public abstract bool MoveJog(SixRobotJoint point, bool bmamual, int Speed, int BlendValue);
        public abstract int SetRunSpeed(int speed);
        public abstract int SetServo(bool b_Servo);
        public abstract int Stop();
        public abstract bool ReadServoStatue();
        public abstract int ResetAlarm();
        public abstract bool IsConnect();

        public virtual bool SetPayload(int Scope, string payloadName)
        {
            return true;
        }
    }

    public class SixRobotMotionMgr
    {
        private Dictionary<string, BaseSixRobot> dicRobot = new Dictionary<string, BaseSixRobot>();
        private static SixRobotMotionMgr _RobotObj = null;
        private static object _lock = new object();
        public static SixRobotMotionMgr GetInstace()
        {

            if (_RobotObj == null)
            {
                lock (_lock)
                {
                    if (_RobotObj == null)
                    {
                        _RobotObj = new SixRobotMotionMgr();
                        return _RobotObj;
                    }
                    else
                        return _RobotObj;
                }
            }
            else
                return _RobotObj;
        }

        public void AddRobot(string strName, string strType, params object[] RobotParam)
        {
            Assembly assembly = Assembly.GetAssembly(typeof(BaseSixRobot));
            string name = "MotionIoLib.SixRobot_" + strType;
            //Motion_Advantech
            Type type = assembly.GetType(name);
            bool flag = type == null;
            if (flag)
            {
                throw new Exception(string.Format($"六轴机器人：{strName} 配置失败，请检查参数"));
            }
            object[] args = new object[RobotParam.Length];
            for (int i = 0; i < RobotParam.Length; i++)
            {
                args[i] = RobotParam[i];
            }

            if (!dicRobot.ContainsKey(strName))
            {
                dicRobot.Add(strName, Activator.CreateInstance(type, strName, args) as BaseSixRobot);
            }
        }
        public BaseSixRobot GetRobot(string strName)
        {
            if (dicRobot.ContainsKey(strName))
            {
                return dicRobot[strName];
            }
            return null;
        }
        public bool InitRobotSystem()
        {
            foreach (var item in Enum.GetValues(typeof(SixRobotAlarm)))
            {
                CommonDlg.AlarmInfoMannage.GetInstance().AddAlarmInfo((int)item, item.ToString());

            }


            foreach (var item in dicRobot)
            {
                if (!item.Value.Open())
                    return false;
            }
            return true;
        }
    }

}